!!ARBvp1.0
# 
# This shader provides sprite-based textured lights.  The weird input params are due to us not using
# 4-part tex coords in hl_xdl...if we had 4-part tex coords in hl_xdl this would be moot!
#
# Inputs:
#
#	Vertex 		- the coordintaes of the light.
#	Normal 		- actually contains RGB color tint information!
#	Color 		- cotains 4-part "params"
#	Tex unit 0 	- lower left of texture to use
#	Tex unit 1 	- size, alpha of light
#
# Outputs:
#
#	Position	- transformed clip space light pos
#	point size	- calculated lite size
#	color		- R and G contain tex coord offset S & T.  A contains alpha of light

ATTRIB	iPos		= vertex.position;
ATTRIB	iCol		= vertex.color;
ATTRIB	iNrm		= vertex.normal;
ATTRIB	iTex0		= vertex.texcoord[0];
ATTRIB	iTex1		= vertex.texcoord[1];

#PARAM	fog_info	= state.fog.params;
PARAM 	mvp[4]      = { state.matrix.mvp };
PARAM 	mv[4]       = { state.matrix.modelview };
PARAM 	mvinvtran[4]= { state.matrix.modelview.invtrans };

PARAM	light_time	= program.local[0];	# percent night, elapsed time, fog mult, fog add
PARAM	light_info	= program.local[1];	# exponent, scale, min size, recip min size
#PARAM	light_info = { 1.5, 1000000.0, 4.0, 0.25 };
PARAM	one_two		= { 1, 2, -1, 63.0 };
PARAM	zero_one	= { 0, 1 };

OUTPUT oPos         = result.position;
OUTPUT oSize 		= result.pointsize;
OUTPUT oCol			= result.color;

TEMP	eye_pos;				# Location of light in eye coordinates - becomes a vector...
TEMP	eye_dir;				# Direction vector of light in eye space.
TEMP	light_size;				# Effective light size (takes into account constants and user value)
TEMP	total_brightness;		# Total brightness of light

#######################################################################################################################

# Transform the light location
DP4   	oPos.x, mvp[0], iPos;	
DP4   	oPos.y, mvp[1], iPos;
DP4   	oPos.z, mvp[2], iPos;
DP4   	oPos.w, mvp[3], iPos;

# Color: XY comes from tex 0 (tex coord offset).  W (alpha) comes from "intensity" of light.

MOV oCol.xy, iTex0;

# Convert the light location to eye-space. 

DP4	eye_pos.x, mv[0], iPos;
DP4	eye_pos.y, mv[1], iPos;
DP4	eye_pos.z, mv[2], iPos;

# Fogging based on eye-space coords
MAD		total_brightness.w, eye_pos.z, light_time.z, light_time.w;
MIN		total_brightness.w, zero_one.y, total_brightness.w;
MAX		total_brightness.w, zero_one.x, total_brightness.w;

# Size of light comes from tex coord 1 S, ends up in the oSize var.
# We calc the eye-space Z and recip-square it, for now at least.
# Do this now before we normalize eye pos and trash our sizing calcs forever.

MUL		light_size.x, light_info.y, iTex1.x;
RCP 	eye_pos.w, -eye_pos.z;
POW		eye_pos.w, eye_pos.w, light_info.x;
MUL		total_brightness.z, eye_pos.w, light_size.x;
MAX 	oSize.x, light_info.z, total_brightness.z;
MUL		total_brightness.z, total_brightness.z, light_info.w;


# Then normalize it to make a vector.  Please note that this vector is TO the lights.

DP3 eye_pos.w, eye_pos, eye_pos;
RSQ eye_pos.w, eye_pos.w;
MUL eye_pos.xyz, eye_pos.w, eye_pos;

# Also convert the light's "official" direction from world to eye space.

DP4	eye_dir.x, mvinvtran[0], iCol;
DP4	eye_dir.y, mvinvtran[1], iCol;
DP4	eye_dir.z, mvinvtran[2], iCol;
MOV eye_dir.w, iCol;

# Now the brightness is the dot product. DPH uses XYZ from eye-pos but adds W from eye_dir
# which is handy - W is the "always on" amount.

DPH	total_brightness.x, -eye_pos, eye_dir;

# Final alpha assembly based on brightness and size

MIN total_brightness.y, one_two.x, total_brightness.z;
MUL total_brightness.x, total_brightness.y, total_brightness.x;
MUL total_brightness.x, total_brightness.y, total_brightness.x;

# Do day-night dimming
ADD total_brightness.z, iPos.x, iPos.y;
ADD total_brightness.z, total_brightness.z, iPos.z;
FRC total_brightness.z, total_brightness.z;
SGE	total_brightness.z,light_time.x,total_brightness.z;
MUL total_brightness.x, total_brightness.x, total_brightness.z;

# Finally mult in with the light's intensity.

MUL total_brightness.x, total_brightness.x, total_brightness.w;
MUL	oCol.w,  total_brightness.x, iTex1.y;

END
